package com.jmb.api.core;

import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.DbConnection;
import com.jmb.api.model.vo.db.DbVO;
import com.jmb.api.service.ProjectService;
import com.jmb.api.vo.project.ProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@Slf4j
@Component
public class Env {
    @Autowired
    private ProjectService projectService;
    public String domainProjectId(Integer projectId, Integer runEnv)  {
        ProjectVo project = projectService.getProjectById(projectId);
        return domain(project,runEnv);
    }
    // 使用调试环境
    public String domain(ProjectVo projectVo,Integer runEnv){
        String host;
        String protocol;
        Integer port;
        if (projectVo == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_PROJECT_NOT);
        }
        if (runEnv == 0){
            host = projectVo.getDevDomain();
            protocol = projectVo.getDevProtocol();
            port = projectVo.getDevPort();
        }
        else if (runEnv == 1){
            host = projectVo.getTestDomain();
            protocol = projectVo.getTestProtocol();
            port = projectVo.getTestPort();
        }
        else if (runEnv == 2){
            host = projectVo.getStgDomain();
            protocol = projectVo.getStgProtocol();
            port = projectVo.getStgPort();
        }
        else if (runEnv == 3){
            host = projectVo.getProdDomain();
            protocol = projectVo.getProdProtocol();
            port = projectVo.getProdPort();
        } else  if (runEnv == 4){
            host = projectVo.getDevDomain();
            protocol = projectVo.getDevProtocol();
            port = projectVo.getDevPort();
        }else {
            throw new LeaseException(ResultCodeEnum.WEB_API_RUN_ENV);
        }
        if (port!=null){
            return protocol + "://" + host + ":" + port;
        }
        else {
            return protocol + "://" + host;
        }
    }
    /**
     * 根据数据源编号 运行环境 返回连接信息
     * @param dbVO dbVO
     * @param runEnv 运行环境 0dev 1test 2stg 3prod 4debug
     * @return 连接信息
     */
    public DbConnection datasource(DbVO dbVO, Integer runEnv)   {
        // 0启动 1禁用
        int status = dbVO.getStatus();
        if (status == 1) {
            log.warn("数据源已被禁用，dbName={}", dbVO.getName());
            throw new LeaseException(ResultCodeEnum.WEB_API_DB_DISABLED);
        }
        String url;
        String username;
        String password;
        if (runEnv == 4) {
            url = dbVO.getUrl();
            username = dbVO.getUsername();
            password = dbVO.getPassword();
        } else if (runEnv == 0) {
            url = dbVO.getDevUrl();
            username = dbVO.getDevUsername();
            password = dbVO.getDevPassword();
        } else if (runEnv == 1) {
            url = dbVO.getTestUrl();
            username = dbVO.getTestUsername();
            password = dbVO.getTestPassword();
        } else if (runEnv == 2) {
            url = dbVO.getStgUrl();
            username = dbVO.getStgUsername();
            password = dbVO.getStgPassword();
        } else if (runEnv == 3) {
            url = dbVO.getProdUrl();
            username = dbVO.getProdUsername();
            password = dbVO.getProdPassword();
        } else {
            throw new LeaseException(ResultCodeEnum.WEB_API_RUN_ENV_EORROR);
        }
        DbConnection connection = new DbConnection();
        connection.setUrl(url);
        connection.setUsername(username);
        connection.setPassword(password);
        return connection;
    }

}
